import * as step from '@ali/flow-step-toolkit'
import process from 'process'
import {setup} from "./setup";
import path from "path";

const FLOW_JOB_TOKEN: string = 'FLOW_JOB_TOKEN'
const MAVEN_SETTING_XML_PATH: string = 'MAVEN_SETTING_XML_PATH'

async function runStep(): Promise<void> {
    const flowJobToken: string | undefined =
        process.env[FLOW_JOB_TOKEN]

    if (flowJobToken == undefined) {
        let errMsg = 'missing FLOW_JOB_TOKEN';
        throw new Error(errMsg)
    }

    const mavenSettingXmlPath: string | undefined =
        process.env[MAVEN_SETTING_XML_PATH]
    if (mavenSettingXmlPath == undefined) {
        let errMsg = 'missing MAVEN_SETTING_XML_PATH';
        throw new Error(errMsg)
    }

    const absolutePath = path.resolve(mavenSettingXmlPath);
    step.info(`absolute mavenSettingXmlPath: ${absolutePath}`)
    await setup(flowJobToken, absolutePath)
}

runStep()
    .then(function () {
        step.info('run step successfully!')
    })
    .catch(function (err: Error) {
        step.error(err.message)
        process.exit(-1)
    })
